/*
Copyright (C) 2016  Marien Raat

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "camera.h"

#include "config.h"

#include <cmath> 

void PlayerCamera::update(sf::Vector2f position,
                          sf::Vector2f ballPosition)
{
    view.setCenter(position);
    view.setSize(windowSize);
}

void BallCamera::update(sf::Vector2f position,
                        sf::Vector2f ballPosition)
{
    sf::Vector2f center = (position + ballPosition) / 2.0f;
    view.setCenter(center);

    float padding = 300.0f;
    float zoomFactor = std::max
        ((std::abs(position.x - ballPosition.x) + padding)
         / windowSize.x,
         (std::abs(position.y - ballPosition.y) + padding)
         / windowSize.y);
    view.setSize(std::max(zoomFactor, 1.0f) * windowSize);
}

void FieldCamera::update(sf::Vector2f position,
                        sf::Vector2f ballPosition)
{
    sf::Vector2f center = FIELD_SIZE / 2.0f;
    view.setCenter(center);

    float padding = 100.0f;
    float scale =
        std::max((FIELD_SIZE.x + padding) / windowSize.x,
                 (FIELD_SIZE.y + padding) / windowSize.y);
    view.setSize(scale * windowSize);
}
